package com.gbiac.modules.monitor.service.impl;

import com.gbiac.modules.monitor.mapper.AttendanceStatisticsMapper;
import com.gbiac.modules.monitor.model.query.AttendanceStatisticsQuery;
import com.gbiac.modules.monitor.model.vo.AttendanceStatisticsClassVO;
import com.gbiac.modules.monitor.model.vo.AttendanceStatisticsSeatVO;
import com.gbiac.modules.monitor.model.vo.AttendanceStatisticsUserVO;
import com.gbiac.modules.monitor.model.vo.AttendanceStatisticsVO;
import com.gbiac.modules.monitor.service.AttendanceStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Description: 考勤统计服务
 * @Author: fengchengguo
 * @Date:   2024-11-30
 * @Version: V1.0
 */
@Service
@Slf4j
public class AttendanceStatisticsServiceImpl implements AttendanceStatisticsService {

    @Resource
    private AttendanceStatisticsMapper attendanceStatisticsMapper;   /**
     * 线程池
     */
    private final Executor executors = new ThreadPoolExecutor(17, 34, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy());


    @Override
    public AttendanceStatisticsVO query(AttendanceStatisticsQuery attendanceStatisticsQuery) {
        String queryDate = attendanceStatisticsQuery.getQueryDate();
        if(queryDate.contains("-")){
            attendanceStatisticsQuery.setFormat("%Y-%m");
        }else {
            attendanceStatisticsQuery.setFormat("%Y");
        }
        AttendanceStatisticsVO attendanceStatisticsVO = new AttendanceStatisticsVO();
        CompletableFuture<Void> query_1 = CompletableFuture.runAsync(() -> {
            List<AttendanceStatisticsUserVO> userList = attendanceStatisticsMapper.queryUserList(attendanceStatisticsQuery);
            attendanceStatisticsVO.setUserVOList(userList);
        },executors);
        CompletableFuture<Void> query_2 = CompletableFuture.runAsync(() -> {
            List<AttendanceStatisticsSeatVO> seatList = attendanceStatisticsMapper.querySeatList(attendanceStatisticsQuery);
            attendanceStatisticsVO.setSeatVOList(seatList);
        },executors);
        CompletableFuture<Void> query_3 = CompletableFuture.runAsync(() -> {
            List<AttendanceStatisticsClassVO> classVOList = attendanceStatisticsMapper.queryClassList(attendanceStatisticsQuery);
            attendanceStatisticsVO.setClassVOList(classVOList);
        },executors);
        CompletableFuture<Void> allFuture = CompletableFuture.allOf(query_1, query_2, query_3);
        try {
            allFuture.get();
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return attendanceStatisticsVO;
    }
}
